<template>
	<view class="bgbox po_rel">
		<un-navbar titleText="聊天设置" :bgImgtype="2">
		</un-navbar>
		<view class="pa24">
			<view class="pa24 bg_white bo_radius22 flex align_center ma_bottom20" @click="gouserinfo">
				<!-- <image :src="store.state.friendinfo[state.targetId].avatar+'?x-oss-process=image/resize,p_30'"></image> -->
				<view class="flex align_center flex1">
					<image
						:src="(store.state.friendinfo[state.targetId]?.avatar || 'https://vpay-static.oss-cn-shenzhen.aliyuncs.com/static/unmanned/images/main/morentouxiang.png' )+'?x-oss-process=image/resize,p_30'"
						class="userimgs"></image>
					<view class="ma_left24 f_s32">
						{{store.state.friendinfo[state.targetId]?.remark_name || state.targetId}}
					</view>
				</view>
				<view class="iconfont i-right f_s24 te_black9"></view>
			</view>

			<view class="pa_lr24 bg_white bo_radius22  ma_bottom20">
				<!-- <view class="flex align_center xians pa_tb30 justify_between" @click="goremarks()">
					<view class="f_s32">设置备注</view>
					<view class="iconfont i-right f_s24 te_black9"></view>
				</view> -->
				<view class="flex align_center xians pa_tb30 justify_between" @click="gosearch2">
					<view class="f_s32">聊天记录</view>
					<view class="iconfont i-right f_s24 te_black9"></view>
				</view>
				<view class="flex align_center pa_tb30 justify_between" @click="state.topshow=true">
					<view class="f_s32">设置聊天背景</view>
					<view class="iconfont i-right f_s24 te_black9"></view>
				</view>
			</view>

			<view class="pa_lr24 bg_white bo_radius22  ma_bottom20">
				<view class="flex align_center xians pa_tb30 justify_between">
					<view class="f_s32">置顶聊天</view>
					<image :src="`../../static/image/chat/switch${state.zhiding?2:1}.png`" class="switchclass"
						@click="state.zhiding=!state.zhiding,changeConversationTopStatus()"></image>
				</view>
				<view class="flex align_center pa_tb30 justify_between">
					<view class="f_s32">消息免扰</view>
					<image :src="`../../static/image/chat/switch${state.darao?2:1}.png`" class="switchclass"
						@click="state.darao=!state.darao,changeConversationNotificationLevels()"></image>
				</view>
			</view>

			<view class="pa_lr24 bg_white bo_radius22  ma_bottom20">
				<view class="flex align_center xians pa_tb30 justify_between" @click="blackedOut">
					<view class="f_s32">拉黑</view>
					<view class="iconfont i-right f_s24 te_black9"></view>
				</view>
				<view class="flex align_center pa_tb30 justify_between" @click="goreport">
					<view class="f_s32">举报</view>
					<view class="iconfont i-right f_s24 te_black9"></view>
				</view>

			</view>

			<view class="pa_lr24 bg_white bo_radius22  ma_bottom20">
				<view class="flex align_center pa_tb30 justify_between" @click="clearfun">
					<view class="f_s32" style="color: #F74B3E;">清空聊天记录</view>
				</view>
			</view>
		</view>


		<view class="modal bottom-modal pa_lr24" style="z-index: 1000;" :class="state.topshow?'show':''">
			<view class="dialog ma24 bg_white show_box bo_radius22 pa_tb30 ma_lr30 pa_lr30" @click.stop>
				<view class="pa_tb30 f_s32 line_bottom flex align_center" @click.stop="senimgfun(1)">
					<image src="../../static/image/chat/paizhaos.png" class="iconimgs ma_right10"></image>从手机相册选择
				</view>
				<view class="pa_tb30 f_s32 flex align_center" @click.stop="senimgfun(2)">
					<image src="../../static/image/chat/xiangces.png" class="iconimgs ma_right10"></image>拍一张
				</view>
				<view class="cancel_btn te_cyan te_center bo_radius10" @click.stop="state.topshow = false">取消</view>
			</view>
		</view>



	</view>
</template>

<script setup lang="ts">
	import { showToast, getscrollviewheight } from '@/utils/utils'
	import { onLoad, onShow, onUnload, onReady } from '@dcloudio/uni-app'
	import { ref, reactive, getCurrentInstance, watch } from 'vue'
	import { friend_info } from '@/api/addressbook'
	import { friend_block, friend_delete } from '@/api/chat'
	import { useStore } from 'vuex'
	import { uploadFile } from "@/utils/request";
	const store = useStore()
	const instance = getCurrentInstance(); // 获取组件实例
	onReady(async () => {

	})


	const changeConversationNotificationLevels = async () => {
		let callback = {
			onConversationNotificationLevelChanged: (res) => {
				console.log('设置免打扰----------', res)
			}
		};
		let code = await store.state.engine.changeConversationNotificationLevel(1, state.targetId, null, state.darao ? 5 : 1, callback);
	}

	const gettop = () => { }


	const changeConversationTopStatus = async () => {
		let callback = {
			onConversationTopStatusChanged: (res) => {
				console.log('置顶----------', res)
			}
		};
		let code = await store.state.engine.changeConversationTopStatus(1, state.targetId, null, state.zhiding, callback);
	}


	const gouserinfo = () => {
		uni.navigateTo({
			url: '/subPackages/addressbook/userinfo/index?portaltype=2&type=1&system_code=' + state.targetId
		})
	}





	const senimgfun = (e) => {
		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: e == 1 ? ['album'] : ['camera'], //从相册选择
			success: function (res) {
				console.log('resresresresresresresres', res);
				if (res.errMsg == "chooseImage:ok") {


					var p = e == 2 ? plus.io.convertLocalFileSystemURL(res.tempFilePaths[0]) : ''

					let localPath = e == 1 ? res.tempFilePaths[0] : 'file:///' + p


					uploadFile(localPath, {}).then((res : any) => {

						let bg_photo = uni.getStorageSync('bg_photo')

						bg_photo[state.targetId] = res.data
						uni.setStorageSync('bg_photo', bg_photo)
						store.commit('setbg_photo', uni.getStorageSync('bg_photo'))

						state.topshow = false

						uni.showToast({
							title: '设置成功',
							icon: 'success',
							duration: 2000
						});
					})


				}
			}
		});


	}

	const goreport = () => {
		uni.navigateTo({
			url: '/pages/chat/report?targetId=' + state.targetId
		})
	}

	const clearfun = () => {
		uni.showModal({
			title: '提示',
			content: '是否清空聊天记录？',
			success: (res) => {
				if (res.confirm) {
					clearMessagess()
				}
			}
		})
	}


	const clearMessagess = async () => {
		let callback = {
			onMessagesCleared: (res) => {
				console.log('清除聊天记录---------',res)
				
				store.commit('deleteallgetMessages')
				
				uni.showToast({
					title: '清空成功',
					icon: 'success',
					duration: 2000
				});
				//...
			}
		};
		let code = await store.state.engine.clearMessages(1, state.targetId, null, 0, 2, callback);
	}

	const gosearch2 = () => {
		uni.setStorageSync('search1', {
			conversation: {
				targetId: state.targetId
			}
		})

		uni.navigateTo({
			url: '/pages/chat/search2'
		})
	}

	// const goremarks =()=>{
	// 	var info = {
	// 		unique_code: state.targetId,
	// 		remark_name: store.state.friendinfo[state.targetId]?.remark_name || state.targetId,
	// 		remark_phone_number: store.state.friendinfo[state.targetId]?,
	// 		description: state.info.description
	// 	}
	// 	uni.navigateTo({
	// 		url: '/subPackages/addressbook/userinfo/remarks?info='+JSON.stringify(info)
	// 	})
	// }

	const navigationinfo = ref<any>(uni.getStorageSync('navigationinfo'))
	onLoad(async (e) => {
		console.log(e.targetId);

		if (e.targetId) {
			state.targetId = e.targetId


			let callback = {
				onSuccess: (res) => {
					if (res.t == 1) {
						state.darao = false
					} else if (res.t == 5) {
						state.darao = true
					}

					console.log('是否免打扰-----------', res)
					//...
				},
				onError: (res) => {
					//...
				}
			};
			let code = await store.state.engine.getConversationNotificationLevel(1, e.targetId, null, callback);
			getzhdiin()

		}
	})



	const getzhdiin = async () => {
		let callback = {
			onSuccess: (res) => {
				console.log('当前的会话信息是-----------', res.t.top)
				state.zhiding = res.t.top || false
				//...
			},
			onError: (res) => {
				//...
			}
		};
		let code = await store.state.engine.getConversation(1, state.targetId, null, callback);
	}

	const removeConversation = async () => {
		let callback = {
			onConversationRemoved: (res) => {
				//...
			}
		};
		let code = await store.state.engine.removeConversation(1, state.targetId, null, callback);

		uni.switchTab({
			url: '/pages/chat/index'
		})
	}

	const state = reactive({
		targetId: null,
		topshow: false,
		darao: false,
		zhiding: false
	})

	const blackedOut = () => {
		uni.showModal({
			title: '加入黑名单',
			content: '加入黑名单，你将不再收到对方的消息。',
			success: (res) => {
				if (res.confirm) {
					friend_block({
						unique_code: state.targetId
					}).then((res : any) => {
						if (res.code == 200) {
							uni.showToast({
								title: '已加入黑名单',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	}
</script>
<style>

</style>

<style scoped lang="scss">
	$ztcolor: #39C7C1;

	.zttecolor {
		color: $ztcolor;
	}

	.ztbgcolor {
		background-color: $ztcolor;
	}

	.userimgs {
		width: 94upx;
		height: 94upx;
		border-radius: 30upx;
	}

	.xians {
		border-bottom: 1upx solid #F2F2F2;
	}

	.switchclass {
		width: 96upx;
		height: 56upx;
	}

	// 性别弹窗
	.show_box {
		width: 702upx;
	}

	.cancel_btn {
		width: 582upx;
		height: 88upx;
		line-height: 88upx;
		background: rgba(57, 199, 193, 0.15);
		margin: 30upx auto;
	}

	.line_bottom {
		border-bottom: 1px solid #F6F6F6;
	}

	.iconimgs {
		width: 40upx;
		height: 40upx;
		margin-left: 200upx;
	}

	.multipleclass {
		width: 30upx;
		height: 30upx;
		border-radius: 50%;
		border: 2upx solid #D0D0D0;
	}

	.multipleclass2 {
		width: 34upx;
		height: 34upx;
	}
</style>